<?php
namespace app\common\model;

use plug\SUtil;

class User extends Base
{
    // 设置当前模型对应的完整数据表名称

    protected $table = 'ddt_user';

    // 主键ID
    protected $pk = 'user_id';

	public function OpenidGetUser($openid,$field = 'user_id,nick_name'){
		return $this->field($field)->where(['wx_openid'=>$openid])->find();
	}

	public function UnionidGetUser($unionid,$field = 'user_id,nick_name'){
		return $this->field($field)->where(['unionid'=>$unionid])->find();
	}

	/**
	 * 注册用户
	 * @param $nickname
	 * @param $sex
	 * @param $headimgurl
	 * @param $openid
	 * @return void
	 */
	public function binduser($nickname,$sex,$headimgurl,$openid,$unionid,$field = 'wx_openid'){
		$ip = SUtil::getIP();
		$data = [
			'utype' => 1,
			'nick_name' => $nickname,
			'sex' => $sex,
			'avatar' => $headimgurl,
			'add_ip' => $ip,
			"{$field}" => $openid,
			'unionid' => $unionid,
			'lastlogin' => time(),
			'loginip' => $ip,
		];
		$res = $this->save($data);
		if(!$res){
			return false;
		}
		return $this->{$this->pk};
	}

	/**
	 * 注册用户
	 * @param $nickname
	 * @param $sex
	 * @param $headimgurl
	 * @param $openid
	 * @return void
	 */
	public function login($userId,$openid,$field){
		$ip = SUtil::getIP();
		$data = [
            "{$field}" => $openid,
			'lastlogin' => time(),
			'loginip' => $ip,
		];
		$res = $this->save($data,['user_id' => $userId]);
		return $res;
	}

    /**
     * @param array $where  // 查询的条件
     * @param string $item  // 查询的参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*')
    {
        $rs = $this
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }

    // 获取分页列表
    function get_list_page($cond,$orderby,$item,$limit)
    {
        $where = null;

        if(!empty($cond['utype'])){
            $where[] = ['utype','=', $cond['utype']];
        }

        if(!empty($cond['is_sales'])){
            $where[] = ['is_sales','=', $cond['is_sales']];
        }

        $rs = $this
            ->field($item)
            ->where($where)
            ->order($orderby)
            ->paginate($limit);

        return $rs;
    }

	/**
	 * 更新用户预定信息数量或者物流信息数量
	 * @param $user_id
	 * @param $type 更新类型：1为新增；2为清零
	 * @param $field 要更新的字段：booking_count、tracking_count
	 * @return bool
	 */
	function UpdateUserMessgeCount($user_id,$type,$field = 'booking'){
		$where = ['user_id'=>$user_id];
		if($type === 1){
			$res = $this
				->where($where)
				->setInc("{$field}_count");
		}elseif($type === 2){
			$res = $this->save(["{$field}_count"=>0],$where);
		}
		return $res;
	}

//    用户兑换商品减少积分
    function reducepoints($user,$integral){
        $condition = [
            "user_id" => $user['user_id'],
        ];
        $rs = $this
            ->where($condition)
            ->setDec('growth_coin',$integral);
        return $rs;
    }
}